package com.zycfc.zsf.boot.mybatis.util;

import java.util.*;
import org.apache.ibatis.session.*;
import org.slf4j.*;

public class BatchExecutorUtil
{
    private static final Logger log;
    private static final int DEFAULT_FLUSH_SIZE = 50000;
    private final SqlSessionFactory sqlSessionFactory;
    private final int flushSize;
    
    public BatchExecutorUtil(final SqlSessionFactory sqlSessionFactory) {
        this(sqlSessionFactory, 50000);
    }
    
    public BatchExecutorUtil(final SqlSessionFactory sqlSessionFactory, final int flushSize) {
        this.sqlSessionFactory = sqlSessionFactory;
        if (flushSize <= 0) {
            this.flushSize = flushSize;
        }
        else {
            this.flushSize = 50000;
        }
    }
    
    public int insert(final Class<?> mapper, final String method, final List<?> objectList) {
        final String insertStatement = mapper.getName() + "." + method;
        int result = 1;
        if (BatchExecutorUtil.log.isInfoEnabled()) {
            BatchExecutorUtil.log.info("\u6279\u91cf\u63d2\u5165\u5f00\u59cb\uff0cinsertStatement=" + insertStatement + "\u6570\u636e\u91cf\uff1a" + objectList.size());
        }
        if (null == objectList || objectList.isEmpty()) {
            result = 0;
            return result;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            for (int index = 0; index < objectList.size(); ++index) {
                sqlSession.insert(insertStatement, (Object)objectList.get(index));
                if ((index > 0 && index % this.flushSize == 0) || index == objectList.size() - 1) {
                    sqlSession.flushStatements();
                }
            }
            Thread.sleep(100L);
            if (BatchExecutorUtil.log.isInfoEnabled()) {
                BatchExecutorUtil.log.info("\u6279\u91cf\u63d2\u5165\u5b8c\u6210\uff0cinsertStatement=" + insertStatement);
            }
        }
        catch (Exception e) {
            result = 0;
            BatchExecutorUtil.log.error("\u6279\u91cf\u63d2\u5165\u5f02\u5e38\uff1a" + e.getMessage(), (Throwable)e);
            if (null != sqlSession) {
                sqlSession.rollback(true);
            }
        }
        finally {
            if (null != sqlSession) {
                sqlSession.close();
            }
        }
        return result;
    }
    
    public int update(final Class<?> mapper, final String method, final List<?> objectList) {
        final String updateStatement = mapper.getName() + "." + method;
        int result = 1;
        if (BatchExecutorUtil.log.isInfoEnabled()) {
            BatchExecutorUtil.log.info("\u6279\u91cf\u66f4\u65b0\u5f00\u59cb\uff0cupdateStatement=" + updateStatement + "\u6570\u636e\u91cf\uff1a" + objectList.size());
        }
        if (null == objectList || objectList.isEmpty()) {
            result = 0;
            return result;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            for (int index = 0; index < objectList.size(); ++index) {
                sqlSession.insert(updateStatement, (Object)objectList.get(index));
                if ((index > 0 && index % this.flushSize == 0) || index == objectList.size() - 1) {
                    sqlSession.flushStatements();
                }
            }
            Thread.sleep(100L);
            if (BatchExecutorUtil.log.isInfoEnabled()) {
                BatchExecutorUtil.log.info("\u6279\u91cf\u66f4\u65b0\u5b8c\u6210\uff0cupdateStatement=" + updateStatement);
            }
        }
        catch (Exception e) {
            result = 0;
            BatchExecutorUtil.log.error("\u6279\u91cf\u66f4\u65b0\u5f02\u5e38\uff1a" + e.getMessage(), (Throwable)e);
            if (null != sqlSession) {
                sqlSession.rollback(true);
            }
        }
        finally {
            if (null != sqlSession) {
                sqlSession.close();
            }
        }
        return result;
    }
    
    public int delete(final Class<?> mapper, final String method, final List<?> objectList) {
        final String deleteStatement = mapper.getName() + "." + method;
        int result = 1;
        if (BatchExecutorUtil.log.isInfoEnabled()) {
            BatchExecutorUtil.log.info("\u6279\u91cf\u5220\u9664\u5f00\u59cb\uff0cdeleteStatement=" + deleteStatement + "\u6570\u636e\u91cf\uff1a" + objectList.size());
        }
        if (null == objectList || objectList.isEmpty()) {
            result = 0;
            return result;
        }
        SqlSession sqlSession = null;
        try {
            sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            for (int index = 0; index < objectList.size(); ++index) {
                sqlSession.insert(deleteStatement, (Object)objectList.get(index));
                if ((index > 0 && index % this.flushSize == 0) || index == objectList.size() - 1) {
                    sqlSession.flushStatements();
                }
            }
            Thread.sleep(100L);
            if (BatchExecutorUtil.log.isInfoEnabled()) {
                BatchExecutorUtil.log.info("\u6279\u91cf\u5220\u9664\u5b8c\u6210\uff0cdeleteStatement=" + deleteStatement);
            }
        }
        catch (Exception e) {
            result = 0;
            BatchExecutorUtil.log.error("\u6279\u91cf\u5220\u9664\u5f02\u5e38\uff1a" + e.getMessage(), (Throwable)e);
            if (null != sqlSession) {
                sqlSession.rollback(true);
            }
        }
        finally {
            if (null != sqlSession) {
                sqlSession.close();
            }
        }
        return result;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)BatchExecutorUtil.class);
    }
}
